package com.seveneight.baijiesteward.ui.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.CardListAdapter;
import com.seveneight.baijiesteward.base.BaseFragmentBack;
import com.seveneight.baijiesteward.base.MessageBean;
import com.seveneight.baijiesteward.bean.CardBean;
import com.seveneight.baijiesteward.ui.user.activity.AddCarActivity;
import com.seveneight.baijiesteward.ui.user.contract.CardListContract;
import com.seveneight.baijiesteward.ui.user.contract.RemoveCardContract;
import com.seveneight.baijiesteward.ui.user.contract.SetDefaultContract;
import com.seveneight.baijiesteward.ui.user.presenter.CardListPresenter;
import com.seveneight.baijiesteward.ui.user.presenter.RemoveCardPresenter;
import com.seveneight.baijiesteward.ui.user.presenter.SetDefaultPresenter;
import com.seveneight.baijiesteward.utils.RecyclerviewInitUtil;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * 暂时弃用-2018.3.27
 * Created by user1 on 2018/2/9.
 */

public class ManagercardFragment extends BaseFragmentBack implements View.OnClickListener, CardListContract.CardListView, RemoveCardContract.RemoveCarView, SetDefaultContract.SetDefaultView {

    SetDefaultContract.SetDefaultPresenter setDefaultPresenter;
    RemoveCardContract.RemoveCardPresenter removeCardPresenter;
    CardListContract.CardListPresenter cardListPresenter;
    private TextView tv_addcard;
    private ImageView iv_back;
    private TextView tv_title;
    private LRecyclerView recyclerview;
    private CardListAdapter adapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private List<CardBean> data = new ArrayList<>();
    private CardBean cardBean;
    private int position;
    private int ADDCARD_CODE = 99;


    @Override
    protected int getContentView() {
        return R.layout.fragment_managercard;
    }

    public static ManagercardFragment newInstance() {
        Bundle args = new Bundle();
        ManagercardFragment fragment = new ManagercardFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        recyclerview = findView(R.id.recyclerview);
        iv_back = findView(R.id.iv_back);
        tv_title = findView(R.id.tv_title);
        tv_title.setText("结算卡管理");
        View footer = LayoutInflater.from(mContext).inflate(R.layout.layout_cardlistbottom, (ViewGroup) view.findViewById(android.R.id.content), false);
        tv_addcard = (TextView) footer.findViewById(R.id.tv_addcard);
        RecyclerviewInitUtil.initLineRecycler(mContext, recyclerview);
        adapter = new CardListAdapter(mContext);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        lRecyclerViewAdapter.addFooterView(footer);
        recyclerview.setAdapter(lRecyclerViewAdapter);
//        EventBus.getDefault().register(this);
    }

    @Override
    protected void initData() {
        setDefaultPresenter = new SetDefaultPresenter(this);
        removeCardPresenter = new RemoveCardPresenter(this);
        cardListPresenter = new CardListPresenter(this);
        getData();
    }

    @Override
    protected void setListener() {
        iv_back.setOnClickListener(this);
        tv_addcard.setOnClickListener(this);
        recyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });

        recyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {

            }
        });
    }

    private void getData() {
        adapter.clear();
        cardListPresenter.getCardList(mContext,jsonFormat.commonParam());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                _mActivity.onBackPressed();
                break;
            case R.id.tv_addcard:
                Bundle bundle = new Bundle();
                bundle.putInt("size", data.size());
                Intent i = new Intent(mContext, AddCarActivity.class);
                i.putExtras(bundle);
                startActivityForResult(i, ADDCARD_CODE);
                break;
        }
    }

    @Override
    public void reBackCardList(List<CardBean> data) {
        this.data = data;
        recyclerview.refreshComplete(data.size());
        adapter.addAll(data);
        lRecyclerViewAdapter.notifyDataSetChanged();
    }

    @Subscribe
    public void onEvent(MessageBean bean) {
        switch (bean.getType()) {
            case 1:
                position = bean.getPos();
                cardBean = (CardBean) bean.getData();
                ActionSheetDialogNoTitle();
                break;
        }
    }

    @Override
    public void reBackRemoveStatus() {
        adapter.remove(position);
    }

    @Override
    public void reBackSetDefaultStatus() {
        getData();
    }

    private void ActionSheetDialogNoTitle() {
        final String[] stringItems = {"删除", "设为默认"};
        final ActionSheetDialog dialog = new ActionSheetDialog(mContext, stringItems, null);
        dialog.isTitleShow(false).show();
        dialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        removeCardPresenter.removeCard(mContext,jsonFormat.setDebitCard(cardBean.getDebitCardID()));
                        break;
                    case 1:
                        setDefaultPresenter.setDefault(mContext,jsonFormat.setDebitCard(cardBean.getDebitCardID()));
                        break;
                }
                dialog.dismiss();
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ADDCARD_CODE && resultCode == RESULT_OK) {
            getData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }

}
